package cn.kent;

import cn.kent.util.Employee;
import cn.kent.util.EmployeeData;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * StreamAPI 测试类
 * 创建Stream的四种方式如下代码
 */
public class StreamAPITest {

    /**
     * 创建Stream的方法：1.通过集合
     */
    @Test
    public void test() {
        final List<Employee> employees = EmployeeData.getEmployees();
        final Stream<Employee> stream = employees.stream();// 获取顺序流
        final Stream<Employee> parallelStream = employees.parallelStream(); // 获取并行流，多线程执行
    }

    /**
     * 2.通过数组
     */
    @Test
    public void test1() {
        int[] arr = new int[]{1, 2, 3, 4, 5, 6};
        // 调用 static IntStream stream(int[] array) 返回一个流
        final IntStream stream = Arrays.stream(arr);

        final Employee tom = new Employee(1001, "Tom");
        final Employee harry = new Employee(1002, "Harry");

        final Employee[] employees = {tom, harry};
        final Stream<Employee> stream1 = Arrays.stream(employees);
    }

    /**
     * 3.通过Stream的of()
     */
    @Test
    public void test2() {
        final Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6);
    }

    /**
     * 4.创建无限流
     */
    @Test
    public void test3() {
        Stream.iterate(0, t -> t + 2).limit(10).forEach(System.out::println);
        Stream.generate(Math::random).limit(10).forEach(System.out::println);
    }
}
